﻿/*code is far away from bug with the animal protecting
*  ┏┓　　　┏┓
*┏┛┻━━━┛┻┓
*┃　　　　　　　┃ 　
*┃　　　━　　　┃
*┃　┳┛　┗┳　┃
*┃　　　　　　　┃
*┃　　　┻　　　┃
*┃　　　　　　　┃
*┗━┓　　　┏━┛
*　　┃　　　┃
*　　┃　　　┃
*　　┃　　　┗━━━┓
*　　┃　神兽保佑　　┣┓
*　　┃　代码无BUG！ ┏┛
*　　┗┓┓┏━┳┓┏┛
*　　　┃┫┫　┃┫┫
*　　　┗┻┛　┗┻┛ 
*　　　
*/

using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections.Generic;
using System.Diagnostics.SymbolStore;
using System.Security.Cryptography;
using System.Text;
using System.Text.Unicode;

namespace Middleware
{
    public static class MethodExtensions
    {
        public static bool IsNullOrWhiteSpace(this string str)
        {
            return string.IsNullOrWhiteSpace(str);
        }

        public static bool IsNullOrEmpty(this string str)
        {
            return string.IsNullOrEmpty(str);
        }
       /// <summary>
       /// AES加密
       /// </summary>
       /// <param name="password"></param>
       /// <param name="uuid"></param>
       /// <returns></returns>
        public  static string EncryPassword(this string password,string uuid)
        {
            byte[] keyArray = Convert.FromBase64String(uuid);
            byte[] toEncryptArray = Encoding.UTF8.GetBytes(password);

            RijndaelManaged rDel = new RijndaelManaged();
            rDel.Key = keyArray;
            rDel.Mode = CipherMode.ECB;
            rDel.Padding = PaddingMode.PKCS7;

            ICryptoTransform cTransform = rDel.CreateEncryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }
        /// <summary>
        /// aes解密 
        /// </summary>
        /// <param name="password"></param>
        /// <param name="uuid"></param>
        /// <returns></returns>
        public static string DecryPassword(this string password,string uuid)
        {
            //byte[] keyArray = Encoding.UTF8.GetBytes(Key);
            byte[] keyArray = Convert.FromBase64String(uuid);
            byte[] toEncryptArray = Convert.FromBase64String(password);

            RijndaelManaged rDel = new RijndaelManaged();
            rDel.Key = keyArray;
            rDel.Mode = CipherMode.ECB;
            rDel.Padding = PaddingMode.PKCS7;

            ICryptoTransform cTransform = rDel.CreateDecryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            return Encoding.UTF8.GetString(resultArray);
        }

    }
}
